/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2018-2022.
 * Description: support debug_ll
 * Author: xiaowei <xiaowei774@huawei.com>
 * Create: 2018-09-17
 */

#include <linux/linkage.h>
#include <linux/init.h>
#include <asm/assembler.h>
#include <asm/memory.h>

__INIT

#include "debug/hisilicon.S"
#include "debug/pl01x.S"

#define UART_TYPE_PL011 0x1 //	index defined by uart_node_matches

#ifdef CONFIG_MMU
	.macro addruart_current, rx, tmp1, tmp2		//temp, pa, va

	mrs     \rx, sctlr_el1
	ands    \rx, \rx, #0x1
	cbz     \rx, 1f
	mov     \rx, \tmp2
	b       2f
1:
	mov     \rx, \tmp1
2:
	.endm
#else  /* !CONFIG_MMU */
	.macro  addruart_current, rx, tmp1, tmp2
	addruart    \tmp1, \tmp2, \rx
	.endm
#endif

     .macro senduart, rd, rx, rt
     cmp \rt, #UART_TYPE_PL011
     b.ne 33f
     senduart_pl011 \rd, \rx
     b   34f
33:  senduart_8250   \rd, \rx
34:
    .endm

    .macro busyuart, rd, rx, rt
     cmp \rt, #UART_TYPE_PL011
     b.ne 35f
     busyuart_pl011 \rd, \rx
     b   36f
35:  busyuart_8250   \rd, \rx
36:
    .endm

ENTRY(printascii)
	addruart_current    x3, x1, x2
	b       2f
1:
	senduart    w1, x3, x4
	busyuart    w2, x3, x4
	cmp     w1, #'\n'
	mov     w1, #'\r'
	b.eq    1b
	b       3f
2:
	cmp     x0, #0
	b.ne    3f
	b       4f
3:
	ldrb    w1, [x0], #1
	cmp     w1, #0
	b.ne    1b
4:
	ret
ENDPROC(printascii)

ENTRY(print_num)
	addruart_current    x3, x1, x2
	mov			w1, #'0'
	senduart    w1, x3, x4
	mov			w1, #'x'
	senduart    w1, x3, x4
	mov			x2, #60
11:
	lsr		x1, x0, x2
	and		w1, w1, #0xf
	cmp		w1, #0xa
	b.eq	15f
	b.ls	12f
	sub		w1, w1, #0xa
	add		w1, w1, #'a'
	senduart	w1, x3, x4
	b		13f
15:
	mov		w1, #'a'
	senduart	w1, x3, x4
	b		13f
12:
	add		w1, w1, #'0'
	senduart	w1, x3, x4
13:
	cmp		x2, #0x0
	b.eq	14f
	sub		x2, x2, #0x4
	b		11b
14:
	mov		w1, #'\n'
	senduart	w1, x3, x4
	ret
ENDPROC(print_num)

__FINIT

__INITDATA
	.align	3
SYM_DATA_START(uart_virt_addr)
	.quad	0
SYM_DATA_END(uart_virt_addr)
__FINITDATA
